package com.pmdb.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.DeferredCommand;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.WindowResizeListener;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalSplitPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.VerticalSplitPanel;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class PMDB implements EntryPoint, WindowResizeListener {


   private static PMDB singleton;

   /**
    * Instantiate an application-level image bundle. This object will provide
    * programmatic access to all the images needed by widgets.
    */
   //

   /**
    * An aggragate image bundle that pulls together all the images for this
    * application into a single bundle.
    */
   /*public interface Images extends Shortcuts.Images, TopPanel.Images {
   }*/

   /**
    * Gets the singleton Mail instance.
    */
   public static PMDB get() {
     return singleton;
   }

   //private TopPanel topPanel = new TopPanel(images);
   private VerticalPanel rightPanel = new VerticalPanel();
   private MovieList movieList;
   private MovieDetail movieDetail;
   //private Shortcuts shortcuts = new Shortcuts(images);

   private VerticalSplitPanel vSplit = new VerticalSplitPanel();
   private HorizontalSplitPanel hSplit = new HorizontalSplitPanel();
   
   private Image cover = null;
   private Image fullCover = null;

   /**
    * This method constructs the application user interface by instantiating
    * controls and hooking up event listeners.
    */
   public void onModuleLoad() {
     singleton = this;
     
     cover = new Image("blank.jpg");
     cover.setWidth("300px");
     cover.setHeight("400px");
     fullCover = new Image("blank.jpg");
     
     vSplit.setSize("400px", "715px");
     //vSplit.setSplitPosition("65%");
     vSplit.setSplitPosition("310px");
     
     //hSplit.setSize("500px", "350px");
     hSplit.setSplitPosition("35%");
     
     movieDetail = new MovieDetail();
     movieDetail.setWidth("100%");
     
     movieList = new MovieList();
     movieList.setWidth("100%");
     
     vSplit.setTopWidget(movieList);
     vSplit.setBottomWidget(cover);


     hSplit.setLeftWidget(vSplit);
     hSplit.setRightWidget(movieDetail);
     
     /*final PopupPanel imagePopup = new PopupPanel(true);
     imagePopup.setAnimationEnabled(true);
     imagePopup.ensureDebugId("cwBasicPopup-imagePopup");
     imagePopup.setWidget(jimmyFull);
     jimmyFull.addClickListener(new ClickListener() {
       public void onClick(Widget sender) {
         imagePopup.hide();
       }
     });*/


     // Hook the window resize event, so that we can adjust the UI.
     Window.addWindowResizeListener(this);

     // Get rid of scrollbars, and clear out the window's built-in margin,
     // because we want to take advantage of the entire client area.
     Window.enableScrolling(false);
     Window.setMargin("0px");

     
     RootPanel.get().add(hSplit);
     
     
     // Call the window resized handler to get the initial sizes setup. Doing
     // this in a deferred command causes it to occur after all widgets' sizes
     // have been computed by the browser.
     DeferredCommand.addCommand(new Command() {
       public void execute() {
         onWindowResized(Window.getClientWidth(), Window.getClientHeight());
       }
     });

     onWindowResized(Window.getClientWidth(), Window.getClientHeight());
   }

   public void onWindowResized(int width, int height) {
     // Adjust the shortcut panel and detail area to take up the available room
     // in the window.
     /*int shortcutHeight = height - shortcuts.getAbsoluteTop() - 8;
     if (shortcutHeight < 1) {
       shortcutHeight = 1;
     }
     shortcuts.setHeight(shortcutHeight + "px");

     // Give the mail detail widget a chance to resize itself as well.
     mailDetail.adjustSize(width, height);*/
   }
   

   /**
    * Displays the specified item.
    * 
    * @param item
    */
   public void displayItem(MovieItem item) {
     movieDetail.setItem(item);
     cover.setUrl(item.getId() + ".jpg");
   }
}
